package com.synergy.stockflow.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Material;
import com.synergy.stockflow.warehouse.entity.InventorySnapshot;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventorySnapshotService;
import com.synergy.stockflow.warehouse.service.WarehouseService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.ibatis.type.SimpleTypeRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.ObjectName;
import javax.management.Query;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory_s")
public class InventorySnapshotController {
    @Autowired
    private InventorySnapshotService inventorySnapshotService;

    //查询数据库
    @GetMapping("/trees")
    private Result<Page<InventoryTransaction>> TreesAll(
            @RequestParam(name = "materialId", required = false) String materialId,
            @RequestParam(name = "warehouseId", required = false) String warehouseId
    ) {
        System.out.println(materialId);
        Page<InventoryTransaction> page = new Page<>(1, 1000);
        page.setRecords(inventorySnapshotService.selectById(page, materialId,warehouseId));
        return Result.success(page);
    }



    @GetMapping
    public Map<String, Object> sag(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "warehouseId", required = false) String warehouseId,
            @RequestParam(name = "batchNo", required = false) String batchNo
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            QueryWrapper<InventorySnapshot> queryWrapper = new QueryWrapper<>();
            if (warehouseId != null && warehouseId.isEmpty()) {
                queryWrapper.lambda().like(InventorySnapshot::getWarehouseId, warehouseId);
            }
            if (batchNo != null && batchNo.isEmpty()) {
                queryWrapper.lambda().like(InventorySnapshot::getBatchNo, batchNo);
            }
            Page<InventorySnapshot> page = new Page<>(current, size);
            result.put("success", true);
            result.put("total", page.getPages());
            result.put("page", page.getPages());
            result.put("date", page.getRecords());
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "分页查询失败:" + a.getMessage());
        }
        return result;
    }

    //新增
    @PutMapping("/insect")
    public Map<String, Object> insect(@RequestBody InventorySnapshot inventorySnapshot) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date now = new Date();
            inventorySnapshot.setCreateTime(now);
            inventorySnapshot.setUpdateTime(now);

            boolean success = inventorySnapshotService.save(inventorySnapshot);
            result.put("success", success);
            result.put("message", success ? "新增成功" : "新增失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增异常" + e.getMessage());
        }
        return result;
    }

    //修改
    @PutMapping("/update")
    private Map<String, Object> update(@RequestBody InventorySnapshot inventorySnapshot) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (inventorySnapshot.getSnapshotId() == null || inventorySnapshot.getSnapshotId().isEmpty()) {
                result.put("success", false);
                result.put("massage", "仓库Id不能为空");
            }
            inventorySnapshot.setUpdateTime(new Date());
            boolean success = inventorySnapshotService.updateById(inventorySnapshot);
            result.put("success", success);
            result.put("message", success ? "修改成功" : "修改失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "修改异常" + e.getMessage());
        }
        return result;
    }

    //删除
    @DeleteMapping("/{id}")
    private Map<String, Object> detele(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = inventorySnapshotService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常" + e.getMessage());
        }
        return result;
    }
}
